package com.hotec.PROJECT_NAME.modules.sys.controller;

import com.google.common.collect.Maps;
import com.hotec.PROJECT_NAME.modules.sys.entity.SysDict;
import com.hotec.PROJECT_NAME.component.basic.BaseController;
import com.hotec.PROJECT_NAME.component.basic.JsonContent;
import com.hotec.PROJECT_NAME.modules.sys.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author llb
 * @date
 */
@RestController
@RequestMapping("sys/dict")
public class SysDictController extends BaseController {
    @Autowired
    SysDictService service;

    /**
     * 根据主键查询对象
     *
     * @param id
     * @return
     */
    @GetMapping("get")
    public JsonContent get(@RequestParam String id) {

        return this.success(service.get(id));
    }

    /**
     * 查询列表
     *
     * @param entity
     * @return
     */
    @PostMapping("query")
    public JsonContent query(@RequestBody SysDict entity) {
        return this.success(service.queryAll(entity));
    }

    /**
     * 查询列表
     *
     * @param entity
     * @return
     */
    @PostMapping("queryPages")
    public JsonContent query(@RequestBody LinkedHashMap entity) {
        return this.success(service.queryByPage(entity,0,10));
    }

    /**
     * 根据code，查询字典项列表
     *
     * @param code
     * @return
     */
    @PostMapping("list/{code}")
    public Map<String,Object> list(@PathVariable String code) {

        Map<String, String> stringStringMap = service.queryChildrenMap(code);
        HashMap<String, Object> result = Maps.newHashMap();
        result.put("data",stringStringMap);
        result.put("code",200);
        return result;

    }
    @PostMapping("lists/{code}")
    public JsonContent listCode(@PathVariable String code) {

        List<SysDict> sysDicts = service.queryChildren(code);
        for (SysDict sysDict : sysDicts) {
            sysDict.setLabel(sysDict.getName());
            sysDict.setValue(sysDict.getCode());
        }

        return JsonContent.success(sysDicts);

    }
    /**
     * 查询所有列表
     * @param entity
     * @return
     */
    @PostMapping("queryTree")
    public JsonContent queryTree(@RequestBody SysDict entity) {
        return this.success(service.queryTree(entity));
    }


    /**
     * 查询所有列表
     * @param entity
     * @return
     */
    @PostMapping("queryTreeByCode")
    public JsonContent queryTreeByCode(@RequestBody SysDict entity) {
//        throw new RuntimeException("系统维护中，请稍后再试！");
        return this.success(service.queryTreeByCode(entity));
    }




    /**
     * 根据对象列表逻辑删除
     *
     * @param entityList
     * @return
     */
    @PostMapping("delete")
    public JsonContent delete(@RequestBody List<SysDict> entityList) {
        service.deleteById(entityList);
        return this.success().setMessage("删除成功！");
    }

    /**
     * 保存更新
     *
     * @return
     */
    @PostMapping("save")
    public JsonContent save(@Valid @RequestBody SysDict entity) {
        service.save(entity);
        return this.success().setMessage("操作成功！");
    }


    /**
     * 根据对象条件，查询单条数据
     *
     * @return
     */
    @PostMapping("getOne")
    public JsonContent getOne(@RequestBody SysDict entity) {
        return this.success(service.getOne(entity));
    }

    /**
     * 查询所有列表
     * @param entity
     * @return
     */
    @PostMapping("queryTreeByCheXing")
    public JsonContent queryTreeByCheXing(@RequestBody SysDict entity) {
        return this.success(service.queryTree(entity.setCode("shipeichexing")));
    }

    /**
     * 查询所有列表
     * @param entity
     * @return
     */
    @PostMapping("queryTreeByPayType")
    public JsonContent queryTreeByPayType(@RequestBody SysDict entity) {
        return this.success(service.queryTree(entity.setCode("payType")));
    }

    /**
     * 查询所有列表
     * @param entity
     * @return
     */
    @PostMapping("queryTreeByOutName")
    public JsonContent queryTreeByOutName(@RequestBody SysDict entity) {
        return this.success(service.queryTree(entity.setCode("elecDeviceType")));
    }


    /**
     * 查询
     * @param entity
     * @return
     */
    @PostMapping("queryTreeByBreakType")
    public JsonContent queryTreeByBreakType(@RequestBody SysDict entity) {
        return this.success(service.queryTree(entity.setCode("breakdownType")));
    }


    /**
     * 查询
     * @param entity
     * @return
     */
    @PostMapping("queryBreakTypeList")
    public JsonContent queryBreakTypeList(@RequestBody SysDict entity) {
        return this.success(service.queryTree(entity.setCode("breakType")));
    }


    /**
     * 查询子集
     * @param parentCode
     * @return
     */
    @GetMapping("child/{parentCode}")
    public JsonContent queryChildByParentCode(@PathVariable String parentCode) {
        return this.success(service.queryChildByParentCode(parentCode));
    }


}
